package 数据结构.结构.树;

public class BinaryTree<Key extends Comparable<Key>,Value> {
    private Node root;
    private int N;

    //插入的方法
    public void put(Key key,Value value){
        root = put(root, key, value);
    }

    public Node put(Node x,Key key,Value value){
        //如果x子树为空
        if(x==null){
            N++;
            return new Node(key,value,null,null);
        }
        //如果X子树不为空
        int cop= key.compareTo(x.key);
        if(cop>0){
            x.right=put(x.right,key,value);
        }else if(cop<0){
            x.left=put(x.left,key,value);
        }else{
            x.value=value;
        }
            return x;
    }

    //查找树中制定key的元素
    public Value get(Key key){
        return get(root,key);
    }

    //从指定的树x中查找key的值
    public Value get(Node x,Key key){
        if(x==null){
            return null;
        }

        int cmp= key.compareTo(x.key);
        if(cmp>0){
            get(x.right,key);
        }else if(cmp<0){
            get(x.left,key);
        }else{
            return x.value;
        }
        return null;
    }

    private class Node{
        public Key key;
        public Value value;
        public Node left;
        public Node right;

        public Node(Key key, Value value, Node left, Node right) {
            this.key = key;
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

    //删除树中key对应的value


}
